#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=600;
typedef int LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL a[maxn];
LL dp[maxn][maxn];
int main(void){
   cin.tie(0);std::ios::sync_with_stdio(false);
   LL n;cin>>n;
   for(LL i=1;i<=n;i++) cin>>a[i];
   memset(dp,0x3f,sizeof(dp));
   for(LL i=1;i<=n;i++){
      dp[i][i]=1;
      if(a[i]==a[i+1]) dp[i][i+1]=1;
      else if(a[i]!=a[i+1]) dp[i][i+1]=2;
   }
   for(LL len=3;len<=n;len++){
      for(LL l=1;l+len-1<=n;l++){
        LL r=l+len-1;
        if(a[l]==a[r]){
            dp[l][r]=min(dp[l][r],dp[l+1][r-1]);
        }
        for(LL k=l;k<=r;k++){
            dp[l][r]=min(dp[l][r],dp[l][k]+dp[k+1][r]);
        }
      }
   }

   cout<<dp[1][n]<<"\n";
   return 0;
}
